package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaFloorConfig;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.seacraft.SeacraftSouthSeaModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_OPEN_PUB, desc = "海上争霸 南海丝路 功勋酒馆挖宝")
public class SeacraftSouthOpenPubCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaOpenPubReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaOpenPubReqMsg.parseFrom(packet.getBytes());
        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }
        // 判断该层需要的道具是否满足
        SeacraftSouthSeaFloorConfig floorConfig = SeacraftSouthSeaMgr.getConfig().getFloorMap().get(reqMsg.getFloor());
        if(floorConfig == null){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_FLOOR_ERROR;
            return;
        }

        if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(floorConfig.getConsume()))){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(floorConfig.getConsume());

        SeacraftSouthSeaProto.SeacraftSouthSeaOpenPubGameToCrossMsg.Builder builder = SeacraftSouthSeaProto.SeacraftSouthSeaOpenPubGameToCrossMsg.newBuilder();
        builder.setActivityId(reqMsg.getActivityId());
        builder.setConsume(floorConfig.getConsume());
        builder.setCtsMsg(reqMsg);

        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_OPEN_PUB, builder, reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaOpenPubRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaOpenPubRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_OPEN_PUB,resp);
        }
    }
}
